package com.cloud.api.game.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.ReactiveListOperations;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.core.ReactiveSetOperations;
import org.springframework.data.redis.core.ReactiveValueOperations;
import org.springframework.stereotype.Service;

import com.cloud.cache.common.CloudCashConstants;
import com.cloud.cache.respository.GameDataRepository;
import com.cloud.cache.respository.GamePlaywayRepository;
import com.cloud.cache.service.CacheConfigTools;
import com.cloud.common.core.BMDataContext.ConfigNames;
import com.cloud.model.GameData;
import com.cloud.model.GamePlayway;

import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Service
@Slf4j
public class GameService {
	@Autowired
	private CacheConfigTools cacheConfigTools;

	@Autowired
	private GameDataRepository gameDataRepository;

	@Autowired
	private ReactiveRedisTemplate<String, GameData> reactiveRedisTemplate;

	@Autowired
	private GamePlaywayRepository gamePlaywayRepository;

//	/**
//	 * 根据游戏配置 ， 选择 返回的 玩法列表
//	 */
//	@Deprecated
//	public Mono<GameData> getGameConfig(String orgi) {
//
//		GameData configData = new GameData();
//		return cacheConfigTools.getGameConfig(orgi).map(gameConfig -> {
//			configData.setGameConfig(gameConfig);
//			return configData;
//		}).then(cacheConfigTools.getAiConfig(orgi).map(aiConfig -> {
//			configData.setAiConfig(aiConfig);
//			return configData;
//		}));
//	}

	/**
	 * 根据游戏配置 ， 选择 返回的 玩法列表
	 */
	public Mono<GameData> getGameData(String orgi) {
		String key = CloudCashConstants.ConfigNames.GAMEDATA.toString();
		ReactiveValueOperations<String, GameData> reactiveValueOperations = reactiveRedisTemplate.opsForValue();

		return reactiveValueOperations.get(key).switchIfEmpty(
				gameDataRepository.findByOrgi(orgi).map(myGameData -> {
						log.info("从mongoDB查询数据!");
						reactiveValueOperations.set(key, myGameData).subscribe();
						return myGameData;
					})
			);
	}

//	public Flux<GamePlayway> findGamePlayway(String orgi) {
//		Pageable pageable = PageRequest.of(0, 100);
//		return gamePlaywayRepository.findByOrgi(orgi, pageable);
//	}

	public Flux<GamePlayway> findGamePlayway(String orgi) {
		Pageable pageable = PageRequest.of(0, 100);
		String key = CloudCashConstants.ConfigNames.GAMECONFIG.toString();
		ReactiveListOperations<String, GamePlayway> reactiveSetOperations = reactiveRedisTemplate.opsForList();
		Flux<GamePlayway> gameFlux = reactiveSetOperations.range(key, 0L, -1L);
		gameFlux.switchIfEmpty(
				gamePlaywayRepository.findByOrgi(orgi).map(playwayList->{
					reactiveSetOperations.leftPushAll(key, playwayList);
					return playwayList;
				})
				);
		
//		return gamePlaywayRepository.findByOrgi(orgi, pageable);
	}
}
